////////////////////////////////////////////////////////////////////////////
//
//  Crytek Engine Source File.
//  Copyright (C), Crytek Studios, 2002.
// -------------------------------------------------------------------------
//  File name:   trackviewnodes.h
//  Version:     v1.00
//  Created:     29/4/2002 by Timur.
//  Compilers:   Visual Studio.NET
//  Description: TrackView's tree control.
// -------------------------------------------------------------------------
//  History:
//
////////////////////////////////////////////////////////////////////////////

#ifndef __trackviewnodes_h__
#define __trackviewnodes_h__

//#include "Controls\MltiTree.h"
#include "Controls/TreeCtrlReport.h"
#include "Controls/ACEdit.h"
#include <IMovieSystem.h>

#if _MSC_VER > 1000
#pragma once
#endif


// forward declarations.
struct IAnimNode;
struct IAnimTrack;
struct IAnimSequence;
class CTrackViewDopeSheetBase;

//////////////////////////////////////////////////////////////////////////
class CTrackViewNodesCtrl : public CTreeCtrlReport
{
public:
	typedef std::vector<IAnimNode*> AnimNodes;

	class CRecord : public CTreeItemRecord
	{
	public:
		CRecord(bool bIsGroup=true, const CString& name="") : m_name(name), 
																	m_bIsGroup(bIsGroup), m_bIsSubTrack(false) {}
		bool IsGroup() const { return m_bIsGroup; }
		const CString& GetName() const { return m_name; }

	public:
		CString m_name;
		bool m_bIsGroup;
		_smart_ptr<CBaseObject> baseObj;
		int paramId;
		_smart_ptr<IAnimNode> node;
		_smart_ptr<IAnimTrack> track;
		bool m_bIsSubTrack;
	};
	typedef CRecord SItemInfo;

	void SetSequence( IAnimSequence *seq );
	void SetKeyListCtrl( CTrackViewDopeSheetBase *keysCtrl );
	void SyncKeyCtrl();
	void ExpandNode( IAnimNode *node );
	void DeleteSelectedNodes();
	void SelectNode( const char *sName, bool bEnsureVisible = false );
	bool CopySelectedNodes();
	void SelectInViewPort();
	bool PasteNodes( IAnimNode *pGroupNode );
	CRecord* GetSelectedNode();

	bool GetSelectedNodes( AnimNodes &nodes );

	void SetEditLock(bool bLock) { m_bEditLock = bLock; }

	float SaveVerticalScrollPos() const;
	void RestoreVerticalScrollPos(float fScrollPos);

	void MoveJustCreatedNodeBelowSelected();

	void Init();

	void AddTrack( int paramId, IAnimNode * node );

public:
	CTrackViewNodesCtrl();

	//////////////////////////////////////////////////////////////////////////
	// Callbacks.
	//////////////////////////////////////////////////////////////////////////
	virtual void Reload();
	virtual void OnFillItems();
	virtual void OnItemExpanded( CXTPReportRow *pRow,bool bExpanded );
	virtual void OnSelectionChanged();
	virtual void OnVerticalScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar);
	virtual bool OnBeginDragAndDrop( CXTPReportSelectedRows* pRows, CPoint point );
	virtual void OnDragAndDrop( CXTPReportSelectedRows* pRows, CPoint absoluteCursorPos );
	virtual void OnItemDblClick( CXTPReportRow *pRow );
	//////////////////////////////////////////////////////////////////////////

protected:
	DECLARE_MESSAGE_MAP()
	afx_msg void OnNMRclick(NMHDR *pNMHDR, LRESULT *pResult);
	afx_msg void OnSize(UINT nType, int cx, int cy);
	afx_msg BOOL OnEraseBkgnd(CDC* pDC);
	afx_msg void OnMouseMove( UINT nFlags, CPoint point );
	afx_msg LRESULT OnFilterChange(WPARAM wParam, LPARAM lParam);
	afx_msg void OnFocusChanging(NMHDR* pNotifyStruct, LRESULT* result);
	afx_msg void OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags);

	virtual BOOL PreTranslateMessage(MSG* pMsg);

	void ShowHideTrack( IAnimNode * node, int trackIndex );
	void RemoveTrack( SItemInfo * pItemInfo );
	void CreateFolder( IAnimNode * pGroupNode );
	void RenameNode( SItemInfo * pItemInfo );
	void EditEvents();
	void SetAsViewCamera();
	void SaveNodeToCollada();
	void LoadNodeFromCollada();

	int ShowPopupMenu(CPoint point, const SItemInfo *pItemInfo );

	void SetPopupMenuLock(CMenu* menu);

	void RecordSequenceUndo();
	void InvalidateNodes();
	
	int GetIconIndexForParam( int nParamId, EAnimNodeType nodeType ) const;
	int GetIconIndexForNode( EAnimNodeType type ) const;

	void FillNodes( CRecord *pRecord, IAnimNode *pParent = NULL );
	void FillTracks( CRecord *pNodeRecord, IAnimNode *pNode );

	bool HasNode(const char *name) const;

	void ExpandNodes(EAnimNodeType nodeType = ANODE_ANYTRACK);
	void CollapseNodes(EAnimNodeType nodeType = ANODE_ANYTRACK);

	//! Set layer node animator for animation control with layer data in the editor.
	void SetLayerNodeAnimators();

	void FillAutoCompletionListForFilter(const IAnimSequence *pSeq);

	bool IsValidReparenting(IAnimNode *pChild, IAnimNode *pParent) const;
	void SetNodeParent(IAnimNode *pChild, IAnimNode *pParent);

	// Utility function for handling material nodes
	// It'll return -1 if the found material isn't a multi-material.
	static int GetMatNameAndSubMtlIndexFromName(CString& matName, const char *nodeName);

	static void UpdateLightAnimationRefs(const CString& oldName, const CString& newName);

	struct CAnimSequenceUndo : public CUndo
	{
		CAnimSequenceUndo( IAnimSequence *pSeq,const char *sName );
	};

protected:
	IAnimSequence *m_sequence;
	CTrackViewDopeSheetBase* m_keysCtrl;

	// Must not be vector, vector may invalidate pointers on add/remove.
	typedef std::vector<SItemInfo*> ItemInfos;
	ItemInfos m_itemInfos;

	class CTrackViewDialog *m_pTrackViewDialog;

	bool m_bEditLock;

	HCURSOR m_downCursor;
	HCURSOR m_upCursor;
	HCURSOR m_downRightCursor;
	HCURSOR m_upRightCursor;

	CACEdit m_filterEdit;
	CStatic m_matchCountStatic;
	UINT m_currentMatchIndex;
	UINT m_matchCount;
};

#endif // __trackviewnodes_h__
